/*
 * 版权所有 (c) 2022-2099 TBIT。保留所有权利。
 */

/*
 * 版权所有 (c) 2022-2099 TBIT。保留所有权利。
 */

package com.tbit.uqbike.service.business;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.tbit.common.utils.TraceUtil;
import com.tbit.uqbike.object.pojo.StockMachine;
import com.tbit.uqbike.object.pojo.Warehouse;
import com.tbit.uqbike.object.pojo.vo.WarehouseVo;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.uqbike.webmanager.dao.core.WarehouseDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
/**
 * 仓库service实现层类
 *
 * <p>
 * 1、步骤1 todo <br>
 * 2、步骤2 todo <br>
 * 3、步骤3 todo
 * </p>
 * @author <a href="mailto:zhang.bingrong@tbit.com.cn ">zhangbingrong</a>
 * @since 2024/1/19
 * @version 1.0
 */
@Service("warehouseService")
public class WarehouseServiceImpl implements WarehouseService {

    @Autowired
    private WarehouseDao warehouseDao;
    @Autowired
    private StockMachineService stockMachineService;
    @Override
    public JsonResult insert(Warehouse warehouse) {
        if (!checkNameLength(warehouse.getWarehouseName())){
            return JsonResult.fail( "仓库名称不得超过6个汉字", TraceUtil.getTraceIdByLocal());
        }
        warehouseDao.insert(warehouse);
        return JsonResult.ok();
    }


    @Override
    public void del(Integer warehouseId) {
        warehouseDao.del(warehouseId);
    }

    @Override
    public Warehouse getByWarehouseId(Integer warehouseId) {
        return warehouseDao.getBywarehouseId(warehouseId);
    }

    @Override
    public JsonResult update(Warehouse warehouse) {
        Warehouse WarehouseByLo =warehouseDao.getWarehouseByName(warehouse.getAccountId(),warehouse.getWarehouseId(),warehouse.getWarehouseName());
        if (WarehouseByLo != null) {
            return JsonResult.fail("仓库名称重复",TraceUtil.getTraceIdByLocal());
        }
        if (!checkNameLength(warehouse.getWarehouseName())){
            return JsonResult.fail("仓库名称不得超过6个汉字",TraceUtil.getTraceIdByLocal());
        }
        //修改仓库名字的同时把入库相关联的也修改了
        UpdateWrapper<StockMachine> updateWrapper = new UpdateWrapper();
        updateWrapper.eq("warehouseId", warehouse.getWarehouseId());
        updateWrapper.set("warehouseName", warehouse.getWarehouseName());
        stockMachineService.update(updateWrapper);
        warehouseDao.update(warehouse);
        return JsonResult.ok();
    }




    @Override
    public List<Warehouse> getByAccountId(Integer accountId,String warehouseName,int size,int page) {
        HashMap<String, Object> queryMap = new HashMap<>();
        queryMap.put("page", (page - 1) * size);
        queryMap.put("size", size);
        queryMap.put("accountId", accountId);
        queryMap.put("warehouseName", warehouseName);
        return warehouseDao.getByAccountId(queryMap);
    }

    @Override
    public int getByAccountIdTotal(Integer accountId,String warehouseName) {
        HashMap<String, Object> queryMap = new HashMap<>();
        queryMap.put("accountId", accountId);
        queryMap.put("warehouseName", warehouseName);
        return warehouseDao.getByAccountIdTotal(queryMap);
    }

    @Override
    public Warehouse getNearWarehouse(Integer accountId, Double lo, Double la,String name) {
        return warehouseDao.getNearWarehouse(accountId,lo,la,name);
    }


    @Override
    public List<WarehouseVo> getAllWarehouse(Integer accountId ) {
        return warehouseDao.getAllWarehouse(accountId);
    }

    // 检查名称长度是否符合要求
    public boolean checkNameLength(String name) {
        // 计算汉字数量
        int chineseCharCount = 0;
        for (int i = 0; i < name.length(); i++) {
            char c = name.charAt(i);
            if (c >= 0x4e00 && c <= 0x9fa5) {
                chineseCharCount++;
            }
        }

        // 判断是否超过限制
        return name.length() <= 12 && chineseCharCount <= 6;
    }

    /**
     * 根据账号平台id批量查询
     *
     * @param accountIdList 区域id
     * @return
     */
    @Override
    public List<Warehouse> getByAccountIds(List<Integer> accountIdList, String warehouseName) {
        return warehouseDao.getByAccountIds(accountIdList);
    }
}
